package org.example.pet.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.pet.annotation.RequiresRole;
import org.example.pet.common.Result;
import org.example.pet.dto.*;
import org.example.pet.pojo.AdoptionApplication;
import org.example.pet.service.AdoptionApplicationService;
import org.example.pet.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/adopt")
public class AdoptionController {
    @Autowired
    private AdoptionApplicationService adoptionApplicationService;

    @GetMapping("/page")
    public Result<PageVO<AdoptionInfoResponseDTO>> page(@ModelAttribute AdoptionPageQueryDTO adoptionPageQueryDTO) {
        log.info("领养信息分页查询...");
        PageVO<AdoptionInfoResponseDTO> page = adoptionApplicationService.page(adoptionPageQueryDTO);
        return Result.success(page);
    }
    
    /**
     * 修改领养信息
     * @param adoptionApplication
     * @return
     */
    @PostMapping("/edit")
    public Result<String> edit(@RequestBody AdoptionApplication adoptionApplication){
        log.info("修改领养信息...");
        adoptionApplicationService.editAdoptionStatus(adoptionApplication);
        return Result.success("修改领养信息成功");
    }

    /**
     * 申请领养
     * @param adoptionApplication
     * @return
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody AdoptionApplication adoptionApplication){
        log.info("申请领养...");
        adoptionApplicationService.save(adoptionApplication);
        return Result.success("申请领养成功");
    }

    /**
     * 删除领养信息
     * @param applicationId
     * @return
     */
    @PostMapping("/delete/{applicationId}")
    public Result<String> delete(@PathVariable("applicationId") Long applicationId){
        log.info("删除领养信息...");
        adoptionApplicationService.delete(applicationId);
        return Result.success("删除领养信息成功");
    }

    /**
     * 查询领养信息详情
     * @param applicationId
     * @return
     */
    @GetMapping("/getAdoptionDetail/{adoptId}")
    public Result<AdoptionApplyInfoDTO> getAdoptionDetail(@PathVariable("adoptId") Long applicationId) {
        log.info("查询领养信息详情 {}...", applicationId);
        AdoptionApplyInfoDTO res = adoptionApplicationService.getAdoptionApplyDetail(applicationId);
        return Result.success(res);
    }

    @GetMapping("/getApplyIdByPetId/{petId}")
    public Result<Long> getApplyIdByPetId(@PathVariable("petId") Long petId) {
        return Result.success(adoptionApplicationService.getApplyIdByPetId(petId));
    }
    
    @GetMapping("/isAdopting/{userId}/{petId}")
    public Result<Boolean> isAdopting(@PathVariable("petId") Long petId, @PathVariable("userId") Long userId) {
        boolean isAdoptingPet = adoptionApplicationService.userIsAdoptingPet(userId, petId);
        return Result.success(isAdoptingPet);
    }
}
